package Core;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Clase con valores comunes a todas las clases
 * @author MusicBox
 * Fecha: Abril 2015
 */
public class Cons {

	public final static String ADMIN_PASS = "63a9f0ea7bb98050796b649e85481845"; // Pass:
																				// root

	public static String DATABASE_NAME;// = "musicbox";
	public static String DATABASE_USER; // = "root";
	public static String DATABASE_PASS; // = "root";
	public static String DATABASE_HOST; // = "localhost";
	public static String DATABASE_PORT; // = "3306";
	public static String DATABASE_DRIVER; // = "mysql";

	public static String SERVER_HOST; // = "127.0.0.1";
	public static String SERVER_PORT;// = "8889";

	/**
	 * Obtiene los datos de configuracion a partir de un fichero
	 */
	public static void populateCons() {
		File fXmlFile = new File("data\\database.config");
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = null;
		try {
			dBuilder = dbFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		}

		Document doc = null;

		try {
			doc = dBuilder.parse(fXmlFile);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		doc.getDocumentElement().normalize();
		NodeList nList = doc.getElementsByTagName("conexion");

		for (int temp = 0; temp < nList.getLength(); temp++) {

			Node nNode = nList.item(temp);

			if (nNode.getNodeType() == Node.ELEMENT_NODE) {

				Element eElement = (Element) nNode;
				DATABASE_NAME = eElement.getElementsByTagName("DATABASE_NAME")
						.item(0).getTextContent();
				DATABASE_USER = eElement.getElementsByTagName("DATABASE_USER")
						.item(0).getTextContent();
				DATABASE_PASS = eElement.getElementsByTagName("DATABASE_PASS")
						.item(0).getTextContent();
				DATABASE_HOST = eElement.getElementsByTagName("DATABASE_HOST")
						.item(0).getTextContent();
				DATABASE_DRIVER = eElement
						.getElementsByTagName("DATABASE_DRIVER").item(0)
						.getTextContent();
				SERVER_HOST = eElement.getElementsByTagName("SERVER_HOST")
						.item(0).getTextContent();
				SERVER_PORT = eElement.getElementsByTagName("SERVER_PORT")
						.item(0).getTextContent();
				DATABASE_PORT = eElement.getElementsByTagName("DATABASE_PORT")
						.item(0).getTextContent();
			}
		}
	}
}
